package com.cfuture08.component.dwz;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cfuture08.component.dwz.vo.Menu;
import com.cfuture08.eweb4j.config.EWeb4JConfig;
import com.cfuture08.eweb4j.jdbc.sql.OrderType;
import com.cfuture08.eweb4j.orm.dao.DAO;
import com.cfuture08.util.BeanXMLUtil;
import com.cfuture08.util.FileUtil;

public class DWZUtil {
	public static DWZJson getSuccessJson(String message,String navTabId, String action, String callType) {
		return new DWZJson("200", message, navTabId, action, "", callType);
	}
	
	public static DWZJson getFailedJson(String message) {
		return new DWZJson("300", message,"", "", "", "reload");
	}

	public static UlTag getUlTag() {
		UlTag root = new UlTag();
		root.setClazz("tree treeFolder");
		List<LiTag> liList = new ArrayList<LiTag>();
		// 1.从数据库中查询没有父亲节点的菜单
		liList = createTree(0);
		root.setLi(liList);
		return root;
	}

	private static List<LiTag> createTree(long id) {
		List<LiTag> liList = new ArrayList<LiTag>();
		// 1.从数据库中查询所有子节点菜单
		List<Menu> menuList = DAO.search(Menu.class, new String[] { "pid" },
				new String[] { String.valueOf(id) }, "rank",
				OrderType.ASC_ORDER);
		if (menuList != null) {
			for (Iterator<Menu> it = menuList.iterator(); it.hasNext();) {
				Menu m = it.next();
				liList.add(new LiTag(new ATag(m.getTarget(), m.getRel(), m
						.getReloadFlag(), m.getHref(), m.getName()), new UlTag(
						"", createTree(m.getId()))));
			}
		}

		return liList;
	}

	public static void main(String[] args) {

		// System.out.println(root.toString());

		String error = EWeb4JConfig.start();
		if (error == null) {
			// System.out.println(DWZUtil.getUlTag());
			try {
				File file = new File(FileUtil.getRootDir(UlTag.class)
						+ "dwz-tree.xml");
				BeanXMLUtil.getBeanXMLWriter(file, DWZUtil.getUlTag()).write();
				// UlTag root = BeanXMLUtil.getBeanXMLReader(file).readOne();
				// System.out.println(root);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.out.println(error);
		}
	}
}
